*****Conscription and Military Service: Do They Result in Future Violent and Non-Violent Incarcerations and Recidivism?  Wang, Flores-Lagunes 2020*****
**This do-file outputs the inmate counts in Table 3

***Install package: Distinct 
ssc inst distinct

*****Please replace here with the directory folder
cd "C:\Users\xinto\Dropbox\Vietnam Veteran Paper\Crime Paper\2020 JHR Data\3. Table 3"

set more off
clear all
set memory 350m
set matsize 1000

program drop _all

**Setting outcome4852 to a crime outcome to drop the observations with missing values; 
**the program counts the total inmates, not inmates with specifc crime outcomes 

global outcomes4852 = "nonviolent_new"

*Change the race4852 to values of 0 or 1 to obtain inmate counts for nonwhites or whites
global race4852 = "0"

use pop_sample_03162020, clear 

keep weight yob Census birthYear white study id total_male_inmate /// 
birthMonth birthDay lott totalSample currentViolent nonviolent vv eligible /// 
contactJuvenile violent_desistence nonviolent_desistence violent_r_draft nonviolent_r_draft ///
violent_drug_draft nonviolent_drug_draft ygroup* birth monthPop drawingYear APNheight ///
seasonYear seasonPop seasonYear_group seasonYear_group* monthYear_group monthYear_group* inmate /// 
fe_yob* fe_mob* fe_yobmob*  yobmob lindo criminal violent nonviolent /// 
mob dob birth ygroup* fe_yob* yobmob fe_mob* drawingYear induction yobRSNID ///
relativeLotteryNumber relLotNumInteract relLotNumSquared relLotNumSqInter yeargroup ///
relLotNumCubed  relLotNumCuInter staylength yob lotteryNumber white study surveyyear /// 
survey1 survey2 survey3

gen nonviolent_new=9
replace nonviolent_new=1 if currentViolent==0 
replace nonviolent_new=0 if currentViolent==1 

do Inmate_counts_program

foreach outcome in $outcomes4852{

          foreach birthgroup in 1  { 
  
                          foreach race in $race4852 {
				 			       
   preserve					
						
   di "Inmate counts in white=`race'"						
   
   gen cohort_sample=. 

   *Note that the cohort_sample also mark out the VSUS records 
   replace cohort_sample=1 if ((white==`race' & inmate==0 & ygroup`birthgroup' ==1 ) /// 
   |( white==`race' & totalSample==1 & ygroup`birthgroup'==1 & `outcome'!=9 & inmate==1 & Census==0))

   replace cohort_sample=0 if cohort_sample==. & (Census==0|inmate==0) 
   
   **** Create birth_cohort variable to mark out the corresponding birth cohort
   gen birth_cohort=.
   replace birth_cohort=1 if white==`race' & ygroup`birthgroup'==1 & (inmate==0 | Census==0) 
   replace birth_cohort=0 if birth_cohort==. & (inmate==0 | Census==0) 
   
   ****Drop the observation of the SIPP data as the population estimates are obtained from other sources  
   drop if Census==1 

   ***svy_male_inmate is the variable with the values of the total inmates incarcerated in state prison-
   * -in 1979, 1986, and 1991, and in the federal prison in 1991 respectively 
   gen svy_male_inmate=total_male_inmate if Census==0 

   gen T=vv
   gen Z=eligible
   gen Y=cohort_sample
   replace Y=0 if Y==. & (Census==0|inmate==0)
   gen W=weight 
   
   /***In the inmate outputs 
   
   num_79, num_86, num_91_state, num_91_fed are the total inmate counts of whites or nonwhites 
   
   num_79_11, num_86_11, num_91_state_11, num_91_fed_11 are the inmate counts of eligible-to-draft (Z=1) veterans (T=1) 
   
   num_79_10, num_86_10, num_91_state_10, num_91_fed_10 are the inmate counts of eligible-to-draft (Z=1) veterans (T=0) 
   
   num_79_01, num_86_01, num_91_state_01, num_91_fed_01 are the inmate counts of eligible-to-draft (Z=0) veterans (T=1) 
   
   num_79_00, num_86_00, num_91_state_00, num_91_fed_00 are the inmate counts of eligible-to-draft (Z=0) veterans (T=0) 
   ***/ 
   Inmate_counts

   restore 
    	       }
			 }
		}
		


   
